import router from '@/router'
import { cloneDeep } from 'lodash'
const loadView = (name) => {
  return () => import(`@v/${name}`)
}
export default {
  namespaced: true,
  state: {
    // 存储 当前 所有的 导航目录菜单
    navs: localStorage.getItem('nav')
      ? JSON.parse(localStorage.getItem('nav'))
      : [],
    // 当前用户可以访问的路由
    authRouter: localStorage.getItem('authRouter')
      ? JSON.parse(localStorage.getItem('authRouter'))
      : []
  },
  mutations: {
    INIT_PERMISSION (state, { userPower, authRouter }) {
      state.navs = userPower
      state.authRouter = authRouter
    },
    ADD_AUTH_ROUTES (state) {
      // addRoute
      console.log(state.authRouter)
      const authRouter = cloneDeep(state.authRouter)
      authRouter.forEach(route => {
        // router.addRoute 动态添加路由
        // 先处理 route component 为字符串
        route.component = loadView(route.component)
        // 动态添加路由
        router.addRoute('layout', route)
      })
      // 添加404路由
    }
  }
}
